﻿using ConfigInfoWebPlatform.API.DBCommon;
using ConfigInfoWebPlatform.Model;
using ConfigInfoWebPlatform.Nlog;
using FileSecretAssemblyStandard;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.Controllers
{
    /// <summary>
    /// 流程处理控制
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MLCfgFileController : ControllerBase
    {
        readonly LogHelper _log;
        readonly string configRootPath;
        readonly string initDB;
        readonly InterfaceFileSecret FileSecret;
        /// <summary>
        /// 流程处理控制
        /// </summary>
        public MLCfgFileController(LogHelper logHelper, IConfiguration configuration, InterfaceFileSecret fileSecret)
        {
            _log = logHelper;
            configRootPath = configuration["AppSetting:configPath"].ToString();
            initDB = configuration["AppSetting:initDB"].ToString();
            FileSecret = fileSecret;
        }
        /// <summary>
        /// 获取机器学习的配置文件信息，以json格式返回，前端以table分页格式显示
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet("mlinfojson")]
        public JsonResult MLInfoJson(string dir,int page,int limit)
        {
            dir = dir.Replace("\\", @"/");
            // 文件总列表
            ConfigFileList MLFileList = new ConfigFileList();
            // 机器学习文件信息
            List<ConfigFileInfoModel> MLCfgFileInfos = new List<ConfigFileInfoModel>();

            // 获取机器学习的文件结构
            DAL_ConfigFile configFile = new DAL_ConfigFile();
            MLCfgFileInfos = configFile.GetConfigFiles(dir);
            if (MLCfgFileInfos != null && MLCfgFileInfos.Count > 0)
            {
                MLFileList.MLCfgFiles = MLCfgFileInfos.Where(a => a.ctype == "F").ToList();
            }

            // *************************获取分页内容数据****************************
            // 返回结果转成json
            List<ConfigFileInfoModel> re = new List<ConfigFileInfoModel>();
            // 获取请求过来的分页页码和每页行数
            //int pageNum = Convert.ToInt16(Request.Form["page"]);
            int pageNum = page;
            //int limitRows = Convert.ToInt16(Request.Form["limit"]);
            int limitRows = limit;
            // 获取页面的数据
            if (MLCfgFileInfos != null && MLCfgFileInfos.Count > 0)
            {
                re = MLFileList.MLCfgFiles.Skip((pageNum - 1) * limitRows).Take(limitRows).ToList();
            }
            // json对应的类
            CfgJsonObject MLCfgJson = new CfgJsonObject();
            MLCfgJson.code = 0;
            MLCfgJson.msg = "sucess";
            if (MLFileList.MLCfgFiles != null && MLFileList.MLCfgFiles.Count > 0)
            {
                MLCfgJson.count = MLFileList.MLCfgFiles.Count;
            }
            else
            {
                MLCfgJson.count = 0;
            }
            MLCfgJson.data = re;
            return new JsonResult(MLCfgJson);
        }


        /// <summary>
        /// 读取机器学习相关文件的具体内容
        /// </summary>
        /// <param name="fileName">文件名称，全路径</param>
        /// <returns></returns>
        [HttpGet("detail")]
        public string Detail(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return "";
            }
            else 
            {
                string filenameFull = Path.Combine(configRootPath, fileName);
                string xmlFileContent = FileSecret.DecryptFileToContent(filenameFull);
                return xmlFileContent;
            }
            
        }


    }
}
